package timesheet.controller;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

import javax.imageio.ImageIO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import timesheet.model.Badging;
import timesheet.model.Event;
import timesheet.model.EventTime;
import timesheet.model.Users;
import timesheet.repository.BadgingRepository;
import timesheet.repository.EventRepository;
import timesheet.repository.EventTimeRepository;
import timesheet.repository.UserRepository;
import timesheet.util.FileUtil;
import timesheet.util.ImageUtil;
import timesheet.util.NotificationUtil;
import timesheet.web.ContextUtil;

@Controller
@Transactional
public class UserListController {
	@Autowired UserRepository userRepository;
	@Autowired EventRepository eventRepository;
	@Autowired EventTimeRepository eventTimeRepository;
	@Autowired BadgingRepository badgingRepository;
	@RequestMapping("/userlist")
	public ModelAndView userlist(){
		if("admin".equals(ContextUtil.getHttpSession().getAttribute("ROLE"))){
			ModelAndView mv = new ModelAndView("userlist");
			List<Users> userlist = userRepository.getAllUser();
			mv.addObject("userlist", userlist);
			return mv;
		}
		else{return new ModelAndView("page-not-found");}
	}
	@RequestMapping("/userdetail")
	public ModelAndView userdetail(@RequestParam("id") Long id){
		//if("admin".equals(ContextUtil.getHttpSession().getAttribute("ROLE"))){
			ModelAndView mv = new ModelAndView("userdetail");
			Users user = userRepository.find(id);
			mv.addObject("user", user);
			mv.addObject("role", user.getRole());
			return mv;
		//}
		//else{return new ModelAndView("page-not-found");}
	}
	@RequestMapping("/userupdate")
	public ModelAndView userupdate(@RequestParam("role") String role, @RequestParam("id") Long id, @RequestParam("password") String password){
		//if("admin".equals(ContextUtil.getHttpSession().getAttribute("ROLE"))){
			String userid = String.valueOf(id);
			Users user = userRepository.find(id);
			user.setRole(role);
			user.setPassword(password);
			userRepository.merge(user);
			if(userid.equals(ContextUtil.getHttpSession().getAttribute("UserId")))
				ContextUtil.getHttpSession().setAttribute("ROLE", role);
			return userlist();
//		}
//		else{return new ModelAndView("page-not-found");}
	}
	
	@RequestMapping("/userdelete")
	public ModelAndView userdelete(@RequestParam("id") Long id){
		//if("admin".equals(ContextUtil.getHttpSession().getAttribute("ROLE"))){
			String userid = String.valueOf(id);
			Users user = userRepository.find(id);
			List<Event> eventlist = eventRepository.getAllEventByUser(user);
			List<EventTime> eventTimeList = eventTimeRepository.getEventTimeByUser(user);
			List<Badging> badgingList = badgingRepository.getAllByUser(user);
			for(Badging b : badgingList){
				badgingRepository.remove(b);
			}
			for(EventTime et : eventTimeList){
				eventTimeRepository.remove(et);
			}
			for(Event e:eventlist){
				eventRepository.remove(e);
			}
			userRepository.delete(user);
			return userlist();
		//}
		//else{return new ModelAndView("page-not-found");}
	}
	
	/*@SuppressWarnings("finally")
	@RequestMapping("/imageaddfromurl")
	public ModelAndView imageaddfromurl(@RequestParam("id") long userid, @RequestParam(value="fileurl") String url) {
		
		Users u = userRepository.find(userid);
		//ModelAndView mv = new ModelAndView("redirect:/bouteille/bottledisplay");
		//mv.addObject("idBottle", bottleid);

		BufferedImage image = null;
		//BufferedImage image2 = null;

		try{
			image = ImageUtil.readImage(url);
			//image2 = ImageUtil.readImage(url);
		}catch (RuntimeException e) {
			NotificationUtil.addNotificationMessage("veuillez indiquer une URL valide");
			return new ModelAndView("page-not-found");//useless to try to save image if we don't have it
		}

		///// Save original image, scale it and save the resized image.
		try {
			
			ByteArrayOutputStream outStream = new ByteArrayOutputStream();
			//ByteArrayOutputStream outStream2 = new ByteArrayOutputStream();
			ImageIO.write(image, "jpg", outStream);
			//ImageIO.write(image2, "jpg", outStream2);
			//String test = FileUtil.getGenFolderPath();
			//ImageUtil.saveImageToFileAsJPEG(image, FileUtil.getGenFolderPath() + FileUtil.USER_ORIGINAL_SUB_FOLDER, u.getId() + ".jpg", 0.9f);

			image = ImageUtil.scale(new ByteArrayInputStream(outStream.toByteArray()),120 * 200, 200, 200);
			
			//image2 = ImageUtil.scale(new ByteArrayInputStream(outStream2.toByteArray()),50 * 50, 50, 50);

			ImageUtil.saveImageToFileAsJPEG(image,  
					FileUtil.getGenFolderPath() + FileUtil.USER_RESIZED_SUB_FOLDER, u.getId() + ".jpg", 0.9f);
			
			//ImageUtil.saveImageToFileAsJPEG(image2, FileUtil.getGenFolderPath() + FileUtil.USER_AVATAR_SUB_FOLDER, u.getId() + ".jpg", 0.9f);

			u.setHasImage(true);
			u.setUrlImage(url);
			userRepository.merge(u);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		finally{
			ModelAndView mv = new ModelAndView("userdetail");
			mv.addObject(userid);
			return mv;
		}
	}*/
}
